<template>
    <el-container class="home_container">

        <!-- 头部区域 -->
        <el-header>
            <div>

                <span>电商后台管理系统</span>
            </div>
            <el-button @click="exit" type="info">退出</el-button>
        </el-header>

        <!-- 页面主体 -->
        <el-container>

            <!-- 侧边栏 -->
            <!-- 折叠起来宽度是64px -->
            <el-aside :width="isCollapse ? '64px' : '200px'"> 
                <!-- 点击关闭菜单栏 -->
                <div class="toggle-button" @click="toggleCollapse">|||</div>
                <!-- 侧边栏菜单区域 -->
                <el-menu
                    background-color="#545c64"
                    text-color="#fff"
                    active-text-color="#409EFF"
                    :unique-opened='true'
                    :collapse='isCollapse'
                    :collapse-transition='false'
                    :router="true"
                    :default-active="activePath"
                >
                <!-- 一级菜单 -->
                    <el-submenu 
                        :index="menu.id.toString()"
                        v-for="menu in menulist" 
                        :key="menu.id"
                    > 
                        <!-- 一级菜单的模板区域 -->
                        <template slot="title">
                        <!-- 图标 -->
                        <i :class="iconsObj[menu.id]"></i>
                        <!-- 文本 -->
                        <span>{{menu.authName}}</span>
                        </template>

                        <!-- 二级菜单 -->
                        <el-menu-item 
                            :index="item.path" 
                            v-for='item in menu.children' 
                            :key="item.id"
                            @click="saveNavState(item.path)"
                        >
                            <template slot="title">
                                <!-- 图标 -->
                                <i class="el-icon-menu"></i>
                                <!-- 文本 -->
                                <span>{{item.authName}}</span>
                            </template>
                        </el-menu-item>                  
                    </el-submenu>                
                </el-menu>
            </el-aside>

            <!-- 右侧内容主体 -->
            <el-main>
                <router-view></router-view>
            </el-main>
        </el-container>

    </el-container>
</template>

<script>
export default {
    name:'Home',
    data(){
        return {
            // 左侧菜单数据
            menulist:[],
            iconsObj:{
                125:'iconfont icon-users',
                103:'iconfont icon-tijikongjian',
                101:'iconfont icon-shangpin',
                102:'iconfont icon-danju',
                145:'iconfont icon-baobiao',
            },
            isCollapse: false,
            activePath:''
            
        }
    },
    // computed:{
    //     activePath(){
    //         return sessionStorage.getItem('navState')
    //     }
    // },

    methods:{
        exit(){
            this.$confirm('确定要退出登录吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                sessionStorage.clear()
                this.$router.push('/login')
                this.$message({
                type: 'success',
                message: '您已退出登录'
                });
            }).catch(() => {
                this.$message({
                type: 'info',
                message: '取消退出'
                });          
            });
        },

        // 获取所有的菜单
        async getMenuList(){
            const {data: res} = await this.$http.get('menus')
            if(res.meta.status !== 200) this.$message.error(res.meta.msg)
            // console.log(res)
            this.menulist = res.data
        },
        // 点击按钮，切换菜单的折叠与展开
        toggleCollapse(){
            this.isCollapse = ! this.isCollapse
        },
        // 点击对应的二级菜单，将菜单的index存入sessionStorage
        saveNavState(path){
            sessionStorage.setItem('navState',path)
            // 解决点击返回之后，点击用户列表不变高亮，因为返回值后根路径为users，点击用户列表不会导致路径发生变化，因此create里的就不会执行，所以需要在这让activePath重新赋值
            this.activePath = path
        }
    },

    created(){
        // 获取所有一级菜单
        this.getMenuList()
        // 获取二级菜单的index，创建的时候就读取navState的内容给activePath，实现记忆高亮效果
        this.activePath = sessionStorage.getItem('navState')
    },

}

</script>

<style lang='less' scoped>
    .home_container {
        height: 100%;
    }
    .el-header {
        background-color: #373d41;
        display: flex;
        justify-content: space-between;
        padding-left: 0;
        align-items: center;
        font-size: 20px;
        >div {
            display: flex;
            align-items: center;
            span {
                margin-left: 15px;
                color: #fff;
            }
        }
    }
    .el-aside {
        background-color: #545c64;
        .el-menu {
            border: 0
        }
    }
    .iconfont{
        margin-right: 10px;
    }
    .el-main {
        background-color: #E8E8E8;
    }
    .toggle-button {
        font-size: 10px;
        text-align: center;
        line-height: 24px;
        background-color: #4a5064;
        color: #fff;
        // 字符间距
        letter-spacing: 0.2em;
        // 鼠标经过变成小手
        cursor: pointer;
    }
</style>